package com.example.xncs;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import android.net.Uri;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FullscreenImageAdapter extends RecyclerView.Adapter<FullscreenImageAdapter.ViewHolder> {

    private AutoPlayActivity activity;
    private List<String> base64ImageList;
    private List<String> imageNames;

    public FullscreenImageAdapter(AutoPlayActivity activity, List<String> base64ImageList, List<String> imageNames) {
        this.activity = activity;
        this.base64ImageList = base64ImageList != null ? base64ImageList : new ArrayList<>();
        this.imageNames = imageNames != null ? imageNames : new ArrayList<>();
        
        android.util.Log.d("FullscreenImageAdapter", "构造函数 - base64ImageList大小: " + this.base64ImageList.size());
        android.util.Log.d("FullscreenImageAdapter", "构造函数 - imageNames大小: " + this.imageNames.size());
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_fullscreen_image, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 边界检查
        if (position < 0 || position >= base64ImageList.size()) {
            android.util.Log.w("FullscreenImageAdapter", "位置越界: " + position + ", 列表大小: " + base64ImageList.size());
            holder.imageView.setImageResource(android.R.drawable.ic_menu_report_image);
            return;
        }
        
        String base64Data = base64ImageList.get(position);

        // 使用base64数据加载图片
        if (base64Data != null && !base64Data.isEmpty()) {
            try {
                // 将base64字符串转换为字节数组
                byte[] imageBytes = Base64.decode(base64Data, Base64.DEFAULT);
                
                // 使用BitmapFactory从字节数组创建Bitmap
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options);
                
                // 计算合适的缩放比例，全屏显示需要更大的图片
                int scale = 1;
                int maxSize = 2048; // 全屏显示使用更大的尺寸
                if (options.outWidth > maxSize || options.outHeight > maxSize) {
                    scale = Math.min(options.outWidth / maxSize, options.outHeight / maxSize);
                    if (scale < 1) scale = 1;
                }
                
                // 实际解码图片
                options.inJustDecodeBounds = false;
                options.inSampleSize = scale;
                Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length, options);
                
                if (bitmap != null) {
                    holder.imageView.setImageBitmap(bitmap);
                } else {
                    // 如果base64解码失败，使用默认图片
                    holder.imageView.setImageResource(android.R.drawable.ic_menu_report_image);
                }
            } catch (Exception e) {
                // 如果base64处理失败，显示错误图片
                holder.imageView.setImageResource(android.R.drawable.ic_menu_report_image);
            }
        } else {
            // 没有base64数据，显示默认图片
            holder.imageView.setImageResource(android.R.drawable.ic_menu_gallery);
        }

        // 设置图片点击事件
        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.toggleControls();
            }
        });
    }

    @Override
    public int getItemCount() {
        int count = base64ImageList.size();
        if (count > 1000) {
            android.util.Log.w("FullscreenImageAdapter", "警告：图片数量异常，当前数量: " + count);
        }
        return count;
    }

    public void setBase64ImageList(List<String> base64ImageList, List<String> imageNames) {
        this.base64ImageList = base64ImageList;
        this.imageNames = imageNames;
        notifyDataSetChanged();
    }

    public String getBase64Data(int position) {
        if (position >= 0 && position < base64ImageList.size()) {
            return base64ImageList.get(position);
        }
        return null;
    }

    public String getImageName(int position) {
        if (position >= 0 && position < imageNames.size()) {
            return imageNames.get(position);
        }
        return "未知图片";
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}